package com.dzpk.gameserver.action.c2s;

import com.dzpk.gameserver.action.CommandEnum;
import com.dzpk.gameserver.module.main.Cache;
import com.dzpk.gameserver.module.main.RoomThread;
import com.dzpk.gameserver.module.main.UserInfo;
import com.dzpk.gameserver.module.main.room.Room;
import com.dzpk.gameserver.module.player.Player;
import com.dzpk.gameserver.protocol.MessageStruct;
import com.dzpk.gameserver.util.BeanControler;
import com.dzpk.gameserver.util.ErrorCode;
import com.dzpk.gameserver.util.Utils;
import com.google.protobuf.InvalidProtocolBufferException;
import org.apache.mina.core.session.IoSession;
import somnus.net.action.AbstractAction;
import somnus.net.message.IMessage;

import java.sql.SQLException;

/**
 * Created by somnus on 16/10/16.
 */
public class C2S_ENTER_ROOM  extends AbstractAction{
    @Override
    public int getMessageId() {
        return CommandEnum.C2S_ENTER_ROOM.getId();
    }

    @Override
    public void execute(IoSession ioSession, IMessage iMessage) throws InvalidProtocolBufferException {
        long l = System.currentTimeMillis();
        MessageStruct.EnterRoom enterRoom = MessageStruct.EnterRoom.parseFrom(iMessage.getData());
        enterRoom.getUserId();
        enterRoom.getToken();
        Player player = Utils.getPlayer(ioSession);
        if(player == null){
            Utils.bindPlayer(Player.createPlayer(ioSession), ioSession);
        }
        Room room = null;
        if(enterRoom.getRoomId() > 0){
            room = Cache.getRoom(enterRoom.getRoomId());
        }else{
            // 创建房间
            room = new Room();
            room.setRoomId(BeanControler.getIDGenrator().generateID());
            room.initRoom();
        }
        if (room == null){
            player.sendErrorMessage(ErrorCode.ROOM_NOT_EXISTS);
            return;
        }
        if (room.getAudMap().size() >= 16 ) {
            logger.debug("房间人数过多...roomId" + room.getRoomId());
            player.sendErrorMessage(ErrorCode.ROOM_MAX_PLAYER);
            return;
        }

        UserInfo userInfo = Cache.getOnlineUserInfo().get(enterRoom.getUserId());
        if(userInfo == null){
            userInfo = new UserInfo();
            userInfo.setPlayer(player);
            userInfo.setUserId(enterRoom.getUserId());
            userInfo.setOnlinetTime(System.currentTimeMillis());

        }
        //

//        UserInfoDao userInfoDao = new UserInfoDaoImp();
//        try {
//            userInfo = userInfoDao.getUserInfo(request.getUserId());
//        } catch (SQLException e) {
//            e.printStackTrace();
//            return pusUser(1);
//        }
//        if (userInfo == null) {
//            logger.debug("没有找到该用户！");
//            return pusUser(1);
//        }

//        //掉线重连并且筹码不够 则当做游客
//        if (reconnect == 0 && leaveRoom.getMin() > userInfo.getChip()) {
//            action = 1;
//        }
        //初始化

        //进入房间完成
        //
//			byte[] bytes = room.getRoomService().getRoomDate(Constant.REQ_GAME_ENTER_ROOM , -1);
        room.getRoomService().accessRoom(userInfo);
        logger.debug(".....进入房间用去的时间: " + (System.currentTimeMillis() - l));

    }

    @Override
    public String getDesc() {
        return CommandEnum.C2S_ENTER_ROOM.getDesc();
    }
}
